<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/10/31
 * Time: 9:32
 */
namespace app\team\model;

use think\Model;
use think\Request;
use think\Db;
use think\Session;

class Order extends Model
{
    /**旅行社订单
     * @param $id
     * @param $where
     * @param $offset
     * @param $limit
     * @return false|\PDOStatement|string|\think\Collection
     */
    static function getOrderList($id,$where,$offset,$limit)
    {
       $res =  Db::name('gtour_order')->alias('g')
            ->field('g.order_id,g.order_sn,g.linkman,g.tel,g.order_price,g.order_status,g.pay_type,g.create_time,gt.gtour_title,t2.create_time as time,t3.admin_rename,g.cancel_code')
            ->join('group_tour gt','g.gtour_id=gt.id','left')
            ->join('order_cancel t2','g.order_id=t2.order_id','left')
            ->join('tour_admin t3','t2.cancel_id = t3.t_admin_id','left')
            ->where('g.team_id',$id)
            ->where('g.is_del',0)
            ->where($where)
            ->limit($offset,$limit)
            ->order('order_id','desc')
            ->select();
       //处理时间
        foreach($res as $k=>$v){
            $res[$k]['create_time']  = date('Y-m-d H:i:s',$v['create_time']);
            $res[$k]['cancel_code'] = substr($v['cancel_code'], -8);
        }
        return $res;
    }

    /**旅行社订单总数
     * @param $id
     * @param $where
     * @return int|string
     */
    static function getOrderCount($id,$where)
    {
        return Db::name('gtour_order')->alias('g')
            ->join('user u','g.user_id=u.uid','left')
            ->join('group_tour gt','g.gtour_id=gt.id','left')
            ->where('gt.team_id',$id)
            ->where($where)
            ->count();
    }

    /**
     * 导出数据
     */
    static function getExcel($id,$where)
    {
        $res =  Db::name('gtour_order')->alias('g')
            ->field('g.order_id,g.order_sn,g.linkman,g.tel,g.order_price,g.order_status,g.pay_type,g.create_time,u.nickname,gt.gtour_title')
            ->join('user u','g.user_id=u.uid','left')
            ->join('group_tour gt','g.gtour_id=gt.id','left')
            ->where('gt.team_id',$id)
            ->where($where)
            ->order('order_id','desc')
            ->select();
        //处理时间
        foreach($res as $k=>$v){
            $res[$k]['create_time']  = date('Y-m-d H:i:s',$v['create_time']);
        }
        return $res;
    }
}